/**
 * 电子秤封装对象
 * @constructor
 */
function ElectronicScale() {
    this.comPort = new ComPort();
}
var weightInfoCallback = undefined;
ElectronicScale.prototype.bindWeightInfoCallback = function(callback){
    weightInfoCallback = callback;
}

/**
 * 打开电子秤设备
 */
ElectronicScale.prototype.openDevice = function () {
    //打开串口
    return this.comPort.open();
    //设置数据回调函数（已有每条指令产生回调）
    // this.comPort.dataCallback = function (dataStr) {
    //     //解析打印信息
    //     var scr = new SCISResponse(dataStr);
    //     // var printInfo = new StandardPrintInfo();
    //     // printInfo.parsing(dataStr);
    //     //返回重量信息对象
    //     if(weightInfoCallback !== undefined){
    //         weightInfoCallback(scr);
    //     }
    // }
}

/**
 * 处理响应数据字符串
 * @param dataStr
 * @param callback
 */
function handlerResponseDataStr(dataStr,callback){
    //解析打印信息
    var scr = new SCISResponse(dataStr);
    //返回重量信息对象
    if(callback !== undefined){
        callback(scr);
    }
}

/**
 * 查询当前净重值
 * @param callback 回调函数，接收命令响应对象SCISResponse
 */
ElectronicScale.prototype.queryNetWeight = function (callback) {
    //及时获取净重值 //S_D_____129.07_kg
    this.comPort.sendData("SI\r\n",function (dataStr){
        handlerResponseDataStr(dataStr,callback);
    });
}

/**
 * 获取皮重值
 * @param callback 回调函数，接收命令响应对象SCISResponse
 */
ElectronicScale.prototype.queryTareWeight = function (callback) {
    //皮重 //TA_A _ _ _ _ _10.00_kg
    this.comPort.sendData("TA\r\n", function (dataStr) {
        handlerResponseDataStr(dataStr, callback);
    });
}

/**
 * 关闭电子秤设备
 */
ElectronicScale.prototype.closeDevice = function () {
    //关闭串口
    this.comPort.close();
}

window.onload = function (ev) {
    window.ElectronicScaleGlobal = new ElectronicScale();
    if(!ElectronicScaleGlobal.openDevice()){
        console.error("电子秤连接失败！");
    }else{
        console.log("电子秤连接成功！");
    }
}
window.onunload = function (ev) {
    window.ElectronicScaleGlobal.closeDevice();
    console.log("关闭电子秤串口");
}
